package tr.richfacesext.components.itemsorter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
import javax.faces.render.Renderer;

import org.apache.commons.lang.StringUtils;

import tr.richfacesext.components.ComponentUtils;

/**
 * 
 * @author mert
 *
 */
public class ItemSorterRenderer extends Renderer {
	
	@SuppressWarnings("unchecked")
	@Override
	public void decode(FacesContext context, UIComponent component) {
		ItemSorter itemSorter = (ItemSorter) component;
		
		if(!itemSorter.isRendered()) {
			return;
		}
		
		Map requestMap = context.getExternalContext().getRequestParameterMap();
		String itemorder = (String) requestMap.get(itemSorter.getId() + "_itemorder");
		if (StringUtils.isNotEmpty(itemorder)) {
			List resultList = new ArrayList();
			String[] items = itemorder.split(",");
			List value = (List) itemSorter.getValue();

			for (String item : items) {
				System.out.println("item : " + item);
				resultList.add(value.get(Integer.parseInt(item)));
			}
			
			itemSorter.setValue(resultList);
		}
	}
	
	public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
		ResponseWriter writer = context.getResponseWriter();
		ItemSorter itemSorter = (ItemSorter) component;
		
		encodeIncludeScript(context, writer, itemSorter);
		encodeMarkup(context, writer, itemSorter);
		encodeScript(context, writer, itemSorter);
	}

	private void encodeIncludeScript(FacesContext context, ResponseWriter writer, ItemSorter itemSorter) throws IOException {
		String contextPath = context.getExternalContext().getRequestContextPath();
		ComponentUtils.encodeIncludeScript(context, writer, itemSorter, contextPath, ItemSorterConstants.JSCRIPT_ITEMSORTER);
		ComponentUtils.encodeIncludeStyle(context, writer, itemSorter, contextPath, ItemSorterConstants.CSS_ITEMSORTER);
	}
	
	private void encodeScript(FacesContext context, ResponseWriter writer, ItemSorter itemSorter) throws IOException {
		writer.startElement("script", itemSorter);
		writer.writeAttribute("type", "text/javascript", null);
		writer.write("\n");
		writer.write("var dragsort = ToolMan.dragsort();\n");
		writer.write("var junkdrawer = ToolMan.junkdrawer();\n");
		writer.write("dragsort.makeListSortable(document.getElementById(\"" + itemSorter.getId() + "_organizer\"));\n");
		writer.write("\n");
		writer.write("function updateOrderOfItems() {\n");
		writer.write("\tupdateOrderOfItemsInternal('" + itemSorter.getId() + "');\n");
		writer.write("}\n");
		writer.endElement("script");
	}

	private void encodeMarkup(FacesContext context, ResponseWriter writer, ItemSorter itemSorter) throws IOException{
		writer.write("<input id=\"" + itemSorter.getId() + "_itemorder\" type=\"hidden\"></input>\n");
		writer.startElement("div", itemSorter);
		writer.writeAttribute("id", itemSorter.getId() + "_album", null);
		writer.writeAttribute("style", "width:" + itemSorter.getWidth() + "px", null);
		writer.write("<ul id=\"" + itemSorter.getId() + "_organizer\" class=\"ulClass\">\n");
		encodeItemsMarkup(context, writer, itemSorter);
		writer.write("</ul>\n");
		writer.endElement("div");
	}
	
	private void encodeItemsMarkup(FacesContext context, ResponseWriter writer, ItemSorter itemSorter) throws IOException {
		Object[] objArr = ComponentUtils.getObjectArr(itemSorter.getValue());
		
		for (int i = 0; i < objArr.length; i++) {
			context.getExternalContext().getRequestMap().put(itemSorter.getVar(), objArr[i]);
			encodeItemMarkupBegin(context, writer, itemSorter, i);
			ComponentUtils.renderChildren(context, itemSorter);
			encodeItemMarkupEnd(writer, itemSorter);
		}
	}

	private void encodeItemMarkupBegin(FacesContext context, ResponseWriter writer, ItemSorter itemSorter, int indice) throws IOException {
		writer.write("<li class=\"liClass\" style=\"width:100px; height:100px\" itemID=\"" + indice + "\">\n");
	}

	private void encodeItemMarkupEnd(ResponseWriter writer, ItemSorter itemSorter) throws IOException {
		writer.write("</li>\n");
	}
	
	public boolean getRendersChildren() {
		return true;
	}
}